package com.kaishengit.web;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.kaishengit.dao.ProdDao;
import com.kaishengit.entity.Prod;
import com.kaishengit.util.StringUtil;

public class ProdServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String method=request.getParameter("m");
		if(StringUtil.isEmpty(method)){
			list(request,response);
		}else if("add".equals(method)){
			String httpMethod=request.getMethod();
			if("GET".equals(httpMethod)){
				toAdd(request,response);
			}else if("POST".equals(httpMethod)){
				add(request,response);
			}
		}else if("edit".equals(method)){
			String httpMethod=request.getMethod();
			if("GET".equals(httpMethod)){
				toEdit(request,response);
			}else if("POST".equals(httpMethod)){
				edit(request,response);
			}
		}else if("del".equals(method)){
			del(request,response);
		}
	}
	private void del(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		
		String id=request.getParameter("id");
		if(new ProdDao().delete(id)){
			response.sendRedirect("prod.jspx");
		}else{
			response.sendRedirect("prod.jspx?code=10007");
		}
	}
	private void edit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		String id=request.getParameter("id");
		Prod prod=new ProdDao().findProdById(id);
		String prodName=request.getParameter("prodName");
		String price=request.getParameter("price");
		if(StringUtil.isEmpty(prodName)||StringUtil.isEmpty(price)){
			request.setAttribute("prod", prod);
			request.setAttribute("code", "10001");
			request.getRequestDispatcher("/WEB-INF/views/edit.jsp").forward(request, response);
		}else{
			try {
				float p=Float.parseFloat(price);
				prod.setProdname(prodName);
				prod.setPrice(p);
				if(new ProdDao().update(prod)){
					response.sendRedirect("prod.jspx");
				}else{
					request.setAttribute("prod", prod);
					request.setAttribute("code", "10002");
					request.getRequestDispatcher("/WEB-INF/views/edit.jsp").forward(request, response);
				}
			} catch (NumberFormatException e) {
				request.setAttribute("prod", prod);
				request.setAttribute("code", "10005");
				request.getRequestDispatcher("/WEB-INF/views/edit.jsp").forward(request, response);
			}
		}
	}
	private void toEdit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		String id=request.getParameter("id");
		if(StringUtil.isEmpty(id)){
			response.sendRedirect("prod.jspx?code=10001");
		}
		Prod prod=new ProdDao().findProdById(id);
		if(prod!=null){
			request.setAttribute("prod", prod);
			request.getRequestDispatcher("/WEB-INF/views/edit.jsp").forward(request, response);
		}else{
			response.sendRedirect("prod.jspx?code=10006");
		}
	}
	private void add(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		String prodName=request.getParameter("prodName");
		String price=request.getParameter("price");
		if(StringUtil.isEmpty(prodName)||StringUtil.isEmpty(price)){
			request.setAttribute("code", "10001");
			request.getRequestDispatcher("/WEB-INF/views/add.jsp").forward(request, response);
		}else{
			try {
				float p=Float.parseFloat(price);
				Prod prod=new Prod();
				prod.setPrice(p);
				prod.setProdname(prodName);
				if(new ProdDao().save(prod)){
					response.sendRedirect("prod.jspx");
				}else{
					request.setAttribute("code", "10004");
					request.getRequestDispatcher("/WEB-INF/views/add.jsp").forward(request, response);
				}
			} catch (NumberFormatException e) {
				request.setAttribute("code", "10005");
				request.getRequestDispatcher("/WEB-INF/views/add.jsp").forward(request, response);
			}
		}
	}
	private void toAdd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		request.getRequestDispatcher("/WEB-INF/views/add.jsp").forward(request, response);
	}
	private void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String code=request.getParameter("code");
		List<Prod> prodList=new ProdDao().getAllProds();
		request.setAttribute("prodList", prodList);
		if(StringUtil.isNotEmpty(code)){
			request.setAttribute("code",code);		
		}
		request.getRequestDispatcher("/WEB-INF/views/main.jsp").forward(request, response);
	}
}
